<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>计算器</title>
    <style>
        body{
            padding: 0;
            margin: 0;
        }
        .main{
            display: flex;
            height: 60vh;
            width: 25vw;
            border: 3px solid #30336b;
            align-items: center;
            flex-direction: column;
            margin: 10px auto;
            background: #686de0;
            border-radius: 10px;
        }
        .main p {
            font-size: 1.3em;
        }
        .main input,select{
            width: 20vw;
            height: 4vh;
            margin-top: 20px;
            font-weight: 2em;
        }
        select option{
            font-size: 2em;
            line-height: 20px;
            color: olivedrab;
            font-weight: bold;
            border-bottom: 1px solid #6ab04c;
        }
        #res{
            width: 10vw;
            height: 5vh;
            font-weight: bold;
            font-size: 2em;
            line-height: 25px;
            margin-top: 10px;
        }
    </style>
</head>
<body>
    <div class="main">
        <p>自制计算器</p>
        <input type="text" name="num1" id="num1" />
        <select name="opt" id="opt">
            <option value="+">+</option>
            <option value="-">-</option>
            <option value="*">*</option>
            <option value="/">/</option>
            <option value="%">%</option>
        </select>
        <input type="text" name="num2" id="num2" />
        <button id="res">=</button>
        <p id="res_info">请开始你的计算</p>
    </div>
</body>
</html>

<script>
    let res = document.getElementById("res");

    res.onclick = function(){
        let num1 = document.getElementById('num1').value;
        let num2 = document.getElementById('num2').value;
        let opt = document.getElementById('opt').value;
        let res_info = document.getElementById('res_info');
        let resS = '';
        let flag = true;
        if(num1.length == 0 || num2.length == 0){
            resS = 'num1或者num2为空';
            res_info.innerHTML = resS;
            return false;
        }
        num1 = parseFloat(num1);
        num2 = parseFloat(num2);
        if(isNaN(num1)){
            resS = 'num1不是数字';
            flag = false;
        }
        if(isNaN(num2)){
            resS = 'num2不是数字';
            flag = false;
        }
        res_info.innerHTML = resS;
        if(flag){
            switch(opt){
                    case '+':
                    resS = 'num1+num的结果=' + (num1 + num2);
                        break;
                    case '-':
                    resS = 'num1-num的结果=' + (num1 - num2);
                        break;
                    case '*':
                    resS = 'num1*num的结果=' + (num1 * num2);
                        break;
                    case '/':
                    resS = 'num1/num的结果=' + (num1 / num2).toFixed(2);
                        break;
                    case '%':
                    resS = 'num1%num的结果=' + (num1 % num2);
                        break;
                    default:
                resS = '请开始你的计算'
            }
             res_info.innerHTML = resS;
        }
    }
</script>
